﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyTruongHoc_App.MyDataSet.QLTHTableAdapters;

namespace QuanLyTruongHoc_App
{
    public partial class QHGVDay : Form
    {
        private int state, idNamHoc;
        private Boolean wasSave;
        private DataTable gvData;

        public QHGVDay()
        {
            InitializeComponent();
        }

        private void QLHinhThucCanBo_Load(object sender, EventArgs e)
        {
            state = PConst.STATE_LOAD;
            // TODO: This line of code loads data into the 'qLTH.view_LopHoc' table. You can move, or remove it, as needed.
            //view_LopHocTableAdapter.Connection.ConnectionString = PublicLib.getStrConn();
            //this.view_LopHocTableAdapter.FillByFlag(this.qLTH.view_LopHoc, PConst.FLAG_NONE);

            // Nam Hoc
            MyDataSet.QLTHTableAdapters.tbl_NamHocTableAdapter namAdap = new MyDataSet.QLTHTableAdapters.tbl_NamHocTableAdapter();
            namAdap.Connection.ConnectionString = PublicLib.getStrConn();
            namAdap.FillByFlag(qLTH.tbl_NamHoc, PConst.FLAG_NONE);

            for (int i = 0; i < qLTH.tbl_NamHoc.Rows.Count; i++)
            {
                tscb_NamHoc.Items.Add(qLTH.tbl_NamHoc.Rows[i][qLTH.tbl_NamHoc.TenColumn].ToString());
            }

            if (tscb_NamHoc.Items.Count > 0)
                tscb_NamHoc.SelectedIndex = tscb_NamHoc.Items.Count - 1;
            // -----------------

            state = PConst.STATE_LOADED;
        }

        private void checkUserPower()
        {
            int pow = PublicLib.getUserPower(PConst.QUYEN_QL_LOP_HOC);
            // user co quyen view
            //tscb_NamHoc.Enabled = ((pow & PConst.POW_VIEW) > 0); // View cac nam hoc cu
            // user co quyen them
            tsb_TaoMoi.Enabled = ((pow & PConst.POW_ADD) > 0);
            // user co quyen edit
            //tsb_Save.Enabled =
            tsb_Edit.Enabled = ((pow & PConst.POW_EDIT) > 0);
            //grc_LopKeTiep.ReadOnly =
            //    grc_GVChuNhiem.ReadOnly = !((pow & PConst.POW_EDIT) > 0);

            // user co delete
            //tsb_Delete.Enabled = ((pow & PConst.POW_DEL) > 0);

            // Quyen phan cong giang day
            pow = PublicLib.getUserPower(PConst.QUYEN_QL_GIANG_DAY);
            // user co quyen view
            tscb_NamHoc.Enabled = ((pow & PConst.POW_VIEW) > 0); // View cac nam hoc cu
            // user co quyen them
            //tsb_TaoMoi.Enabled = ((pow & PConst.POW_ADD) > 0);

            // user co quyen edit
            tsb_Edit.Enabled = 
                tsb_Save.Enabled = ((pow & PConst.POW_EDIT) > 0);
            dg_DanhSach.ReadOnly = !((pow & PConst.POW_EDIT) > 0);
            //tsb_Edit.Enabled = ((pow & PConst.POW_EDIT) > 0);
            //grc_LopKeTiep.ReadOnly =
            //    grc_GVChuNhiem.ReadOnly = !((pow & PConst.POW_EDIT) > 0);

            // user co delete
            //tsb_Delete.Enabled = ((pow & PConst.POW_DEL) > 0);
        }

        private void refreshData()
        {
            // load Giao Vien
            gvData = new DataTable();
            gvData.Columns.Add("id", Type.GetType("System.Int32"));
            gvData.Columns.Add("ten", Type.GetType("System.String"));

            view_GiaoVienTableAdapter gvAdap = new view_GiaoVienTableAdapter();
            tbl_MonHocTableAdapter monAdap = new tbl_MonHocTableAdapter();
            tbl_LopHocTableAdapter lopAdap = new tbl_LopHocTableAdapter();
            monAdap.Connection.ConnectionString =
                lopAdap.Connection.ConnectionString =
                    gvAdap.Connection.ConnectionString = 
                        PublicLib.getStrConn();

            if (tscb_NamHoc.SelectedIndex == tscb_NamHoc.Items.Count - 1)
            {
                gvAdap.FillByFlag(qLTH.view_GiaoVien, PConst.FLAG_NONE);
                monAdap.FillByFlag(qLTH.tbl_MonHoc, PConst.FLAG_NONE);
                lopAdap.FillByFlag(qLTH.tbl_LopHoc, PConst.FLAG_NONE);
            }
            else
            {
                gvAdap.Fill(qLTH.view_GiaoVien);
                monAdap.Fill(qLTH.tbl_MonHoc);
                lopAdap.Fill(qLTH.tbl_LopHoc);
            }

            for (int i = 0; i < qLTH.view_GiaoVien.Rows.Count; i++)
            {
                gvData.Rows.Add(qLTH.view_GiaoVien.Rows[i][qLTH.view_GiaoVien.idColumn],
                                qLTH.view_GiaoVien.Rows[i][qLTH.view_GiaoVien.HoDemColumn].ToString() + " " +
                                qLTH.view_GiaoVien.Rows[i][qLTH.view_GiaoVien.TenColumn].ToString() + " (" +
                                PublicLib.formatGVId((int)qLTH.view_GiaoVien.Rows[i][qLTH.view_GiaoVien.idColumn]) + ")");
            }

            dg_DanhSach.Rows.Clear();
            dg_DanhSach.Columns.Clear();

            dg_DanhSach.Columns.Add("grc_stt", "STT");
            dg_DanhSach.Columns[0].Width = 50;
            dg_DanhSach.Columns[0].ReadOnly = true;
            dg_DanhSach.Columns.Add("grc_monHoc", "Môn học");
            dg_DanhSach.Columns[1].Width = 150;
            dg_DanhSach.Columns[1].ReadOnly = true;
            for (int i = 0; i < qLTH.tbl_LopHoc.Rows.Count; i++)
            {
                DataGridViewComboBoxColumn nCol = new DataGridViewComboBoxColumn();
                nCol.HeaderText = qLTH.tbl_LopHoc.Rows[i][qLTH.tbl_LopHoc.TenLopColumn].ToString();
                nCol.DataSource = gvData;
                nCol.DisplayMember = "ten";
                nCol.ValueMember = "id";
                //nCol.AutoSizeMode = DataGridViewAutoSizeColumnMode.;
                nCol.SortMode = DataGridViewColumnSortMode.Automatic;

                dg_DanhSach.Columns.Add(nCol);
            }

            dg_DanhSach.Rows.Add(qLTH.tbl_MonHoc.Rows.Count);

            idNamHoc = (int)qLTH.tbl_NamHoc.Rows[tscb_NamHoc.SelectedIndex][qLTH.tbl_NamHoc.idColumn];
            MyDataSet.QLTHTableAdapters.qh_GVDayTableAdapter dayAdap = new qh_GVDayTableAdapter();
            dayAdap.Connection.ConnectionString = PublicLib.getStrConn();
            dayAdap.FillByNamHoc(qLTH.qh_GVDay, idNamHoc);
            
            for (int i = 0; i < qLTH.tbl_MonHoc.Rows.Count; i++)
            {
                dg_DanhSach[0, i].Value = i + 1; // stt
                dg_DanhSach[1, i].Value = qLTH.tbl_MonHoc.Rows[i][qLTH.tbl_MonHoc.TenMonHocColumn]; // Ten mon hoc

                int idMonHoc = (int)qLTH.tbl_MonHoc.Rows[i][qLTH.tbl_MonHoc.idColumn];

                for (int j = 0; j < qLTH.tbl_LopHoc.Rows.Count; j++)
                {
                    int idLop = (int)qLTH.tbl_LopHoc.Rows[j][qLTH.tbl_LopHoc.idColumn];
                    DataRow[] tmpRow = qLTH.qh_GVDay.Select(qLTH.qh_GVDay.idMonHocColumn.ColumnName + "=" + idMonHoc + " AND " +
                                                            qLTH.qh_GVDay.idLopHocColumn.ColumnName + "=" + idLop);

                    if (tmpRow.Length == 0 || tmpRow[0][qLTH.qh_GVDay.idGiaoVienColumn] == DBNull.Value)
                        dg_DanhSach[j + 2, i].Value = DBNull.Value;
                    else
                    {
                        //dg_DanhSach[j + 2, i].Value = tmpRow[0][qLTH.qh_GVDay.idGiaoVienColumn];
                        int idGV = (int)tmpRow[0][qLTH.qh_GVDay.idGiaoVienColumn];
                        DataRow[] sRow = gvData.Select("id=" + idGV);
                        if (sRow.Length > 0)
                        {
                            dg_DanhSach[j + 2, i].Value = sRow[0]["id"];
                        }
                    }
                }

                if (i % 2 == 1)
                    dg_DanhSach.Rows[i].DefaultCellStyle.BackColor = PConst.COLOR_BACK_1;
            }

            wasSave = true;
        }

        private void tsb_TaoMoi_Click(object sender, EventArgs e)
        {
            AddEditLopHoc frm_AddLop = new AddEditLopHoc();
            frm_AddLop.MdiParent = this.MdiParent;
            frm_AddLop.Show();
            frm_AddLop.Disposed += new EventHandler(frm_AddLop_Disposed);
        }

        private void bt_Sua_Click(object sender, EventArgs e)
        {
            if (dg_DanhSach.SelectedRows.Count == 0)
                return;
            //dg_DanhSach.SelectedRows[0].Cells[grc_ChuNhiem.Index].Selected = true;
            dg_DanhSach.BeginEdit(true);

            /*int i = int.Parse(dg_DanhSach.SelectedRows[0].Cells[grc_id.Name].Value.ToString());
            AddEditLopHoc frm_EditMH = new AddEditLopHoc();
            frm_EditMH.MdiParent = this.MdiParent;
            frm_EditMH.Show();
            frm_EditMH.setEditInfo(i);
            frm_EditMH.Disposed += new EventHandler(frm_AddLop_Disposed);*/
        }

        private void frm_AddLop_Disposed(object sender, EventArgs e)
        {
            refreshData();
        }

        private void bt_Xoa_Click(object sender, EventArgs e)
        {
            /*if (dg_DanhSach.SelectedRows.Count <= 0)
                return;
            if (MessageBox.Show(MSG.MSG_DELETE, "", MessageBoxButtons.YesNoCancel) == DialogResult.Yes)
            {
                deleteByID(dg_DanhSach.SelectedRows[0].Cells[grc_id.Name].Value.ToString());
            }*/
        }

        private void deleteByID(String _id)
        {
        
            try
            {
                PublicLib.updateField(qLTH.tbl_LopHoc.TableName,
                                        qLTH.tbl_LopHoc.idColumn.ColumnName,
                                        _id,
                                        qLTH.tbl_LopHoc.flagColumn.ColumnName,
                                        PConst.FLAG_DELETE.ToString());
                refreshData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(MSG.MSG_ERROR_INSERT_DB + " " + ex.Message);
            }
        }

        private void QLLoaiDiem_Resize(object sender, EventArgs e)
        {
            dg_DanhSach.Width = this.Width - 2;
            dg_DanhSach.Height = stts_Bottom.Top - dg_DanhSach.Top - 1;
        }

        private void dg_DanhSach_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            bt_Sua_Click(tsb_Edit, new EventArgs());
        }

        private void saveData()
        {
            if (tscb_NamHoc.SelectedIndex == -1)
                return;
            idNamHoc = (int)qLTH.tbl_NamHoc.Rows[tscb_NamHoc.SelectedIndex][qLTH.tbl_NamHoc.idColumn];
            MyDataSet.QLTHTableAdapters.qh_GVDayTableAdapter dayAdap = new qh_GVDayTableAdapter();
            dayAdap.Connection.ConnectionString = PublicLib.getStrConn();
            dayAdap.FillByNamHoc(qLTH.qh_GVDay, idNamHoc);

            for (int i = 0; i < qLTH.tbl_MonHoc.Rows.Count; i++)
            {
                //dg_DanhSach[0, i].Value = i + 1; // stt
                //dg_DanhSach[1, i].Value = qLTH.tbl_MonHoc.Rows[i][qLTH.tbl_MonHoc.TenMonHocColumn]; // Ten mon hoc

                int idMonHoc = (int)qLTH.tbl_MonHoc.Rows[i][qLTH.tbl_MonHoc.idColumn];

                for (int j = 0; j < qLTH.tbl_LopHoc.Rows.Count; j++)
                {
                    int idLop = (int)qLTH.tbl_LopHoc.Rows[j][qLTH.tbl_LopHoc.idColumn];
                    DataRow[] tmpRow = qLTH.qh_GVDay.Select(qLTH.qh_GVDay.idMonHocColumn.ColumnName + "=" + idMonHoc + " AND " +
                                                            qLTH.qh_GVDay.idLopHocColumn.ColumnName + "=" + idLop);

                    if (dg_DanhSach[j + 2, i].Value == DBNull.Value)
                    {
                        if (tmpRow.Length > 0)
                        {
                            tmpRow[0].Delete();
                            dayAdap.Update(tmpRow[0]);
                        }
                    }
                    else
                    {
                        if (tmpRow.Length > 0)
                        {
                            tmpRow[0][qLTH.qh_GVDay.idGiaoVienColumn] = dg_DanhSach[j + 2, i].Value;
                            dayAdap.Update(tmpRow[0]);
                        }
                        else
                        {
                            DataRow nRow = qLTH.qh_GVDay.NewRow();
                            nRow[qLTH.qh_GVDay.idNamHocColumn] = idNamHoc;
                            nRow[qLTH.qh_GVDay.idLopHocColumn] = idLop;
                            nRow[qLTH.qh_GVDay.idMonHocColumn] = idMonHoc;
                            nRow[qLTH.qh_GVDay.idGiaoVienColumn] = dg_DanhSach[j + 2, i].Value;
                            qLTH.qh_GVDay.Rows.Add(nRow);
                            dayAdap.Update(nRow);
                        }
                    }
                }
            }

            wasSave = true;
        }

        private void tsb_Save_Click(object sender, EventArgs e)
        {
            dg_DanhSach.EndEdit();
            saveData();
        }

        private void tscb_Lop_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (state == PConst.STATE_LOAD)
                return;
            if (!wasSave)
                if (MessageBox.Show(MSG.MSG_SAVE, "", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    saveData();
            refreshData();
        }

        private void dg_DanhSach_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            wasSave = false;
        }

        private void QHLopMonHoc_Shown(object sender, EventArgs e)
        {
            refreshData();
        }

        private void tsb_Reset_Click(object sender, EventArgs e)
        {
            refreshData();
        }

        private void dg_DanhSach_Sorted(object sender, EventArgs e)
        {
            for (int i = 0; i<dg_DanhSach.Rows.Count; i++)
                if (i % 2 == 1)
                    dg_DanhSach.Rows[i].DefaultCellStyle.BackColor = PConst.COLOR_BACK_1;
                else
                    dg_DanhSach.Rows[i].DefaultCellStyle.BackColor = PConst.COLOR_BACK_DEF;
        }

        private void QHGVDay_Activated(object sender, EventArgs e)
        {
            checkUserPower();
        }

        private void tsb_In_Click(object sender, EventArgs e)
        {
            PrintDataGrid.PrintDGV print = new PrintDataGrid.PrintDGV();
            print.Print_DataGridView(dg_DanhSach);
        }
    }
}
